package com.dnake.fragment;

import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.SeekBar;
import android.widget.TextView;

import com.dnake.Constant;
import com.dnake.entity.EventSettingBean;
import com.dnake.panel.R;
import com.wx.android.common.util.SharedPreferencesUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.Bind;
import butterknife.ButterKnife;
import me.yokeyword.fragmentation.SupportFragment;

/**
 * @author haoyuliu
 * @since 2017/7/18
 */

public class VoiceFragment extends SupportFragment {
    @Bind(R.id.sb_score)
    SeekBar sbScore;
    @Bind(R.id.sb_papers_score)
    SeekBar sbPapersScore;
    private View mView;
    private AudioManager mAudioManager;
    public static VoiceFragment newInstance() {
        return new VoiceFragment();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_voice, container, false);
        ButterKnife.bind(this, mView);
        initData();
        return mView;
    }

    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    private void initData() {
        mAudioManager = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);
        int streamMax = mAudioManager.getStreamMaxVolume( AudioManager.STREAM_VOICE_CALL );
        int streamCurrent = mAudioManager.getStreamVolume( AudioManager.STREAM_VOICE_CALL );
        int max = mAudioManager.getStreamMaxVolume( AudioManager.STREAM_MUSIC );
        int current = mAudioManager.getStreamVolume( AudioManager.STREAM_MUSIC );

        double call = streamCurrent*100/streamMax;
        double music = current*100/max;

        sbScore.setProgress((int )call);
        sbPapersScore.setProgress((int )music);
    }

    @Override
    public boolean onBackPressedSupport() {
        return super.onBackPressedSupport();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventSetting(EventSettingBean eventBusBean) {
        if (eventBusBean.getCode() == Constant.EVENTBUS_VOICE_SET) {
           if ("1".equals(eventBusBean.getName())) {
               mAudioManager.adjustStreamVolume(
                       AudioManager.STREAM_VOICE_CALL,
                       AudioManager.ADJUST_RAISE,
                       AudioManager.FX_FOCUS_NAVIGATION_UP);
               initData();
            } else if ("7".equals(eventBusBean.getName())) {
               mAudioManager.adjustStreamVolume(
                       AudioManager.STREAM_VOICE_CALL,
                       AudioManager.ADJUST_LOWER,
                       AudioManager.FX_FOCUS_NAVIGATION_UP);
               initData();
            }else if ("2".equals(eventBusBean.getName())){
               mAudioManager.adjustStreamVolume(
                       AudioManager.STREAM_MUSIC,
                       AudioManager.ADJUST_RAISE,
                       AudioManager.FX_FOCUS_NAVIGATION_UP);
               initData();
            }else if ("8".equals(eventBusBean.getName())){
               mAudioManager.adjustStreamVolume(
                       AudioManager.STREAM_MUSIC,
                       AudioManager.ADJUST_LOWER,
                       AudioManager.FX_FOCUS_NAVIGATION_UP);
               initData();
            }
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
